﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LabaCS4
{
    public partial class Show : Form
    {

        public ArrayList arTheBook = null;
        public Main main;



        public Show()
        {
            InitializeComponent();
            arTheBook = new ArrayList();
            UpdateGrid();


        }


        public void вырезатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            main.bufbook = main.cbook;
            int ind = dataGridView1.SelectedCells[0].RowIndex;
            if (ind >= 0 && ind < arTheBook.Count)
            {

                arTheBook.RemoveAt(ind);
                UpdateGrid();
            }


        }



        public void вставитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (main.bufbook != null)
            {
                arTheBook.Add(main.bufbook);
                UpdateGrid();

            }


        }



        public void UpdateGrid()
        {
            if (arTheBook != null)
            {
                DataTable invertory = new DataTable("Invertory");

                DataColumn number = new DataColumn("№");
                DataColumn title = new DataColumn("Название");
                DataColumn name = new DataColumn("Автор");
                DataColumn cost = new DataColumn("Стоимость");
                DataColumn image = new DataColumn("Изображение");



                invertory.Columns.Add(number);
                invertory.Columns.Add(name);
                invertory.Columns.Add(title);
                invertory.Columns.Add(cost);
                invertory.Columns.Add(image);




                int ibook = 0;
                foreach (Book b in arTheBook)
                {
                    DataRow newRow;
                    newRow = invertory.NewRow();
                    newRow["№"] = ibook;
                    newRow["Автор"] = b.author;
                    newRow["Название"] = b.title;
                    newRow["Стоимость"] = b.cost;
                    //newRow["Изображение"] = button1.Click;

                    invertory.Rows.Add(newRow);
                    ibook++;
                }

                dataGridView1.DataSource = invertory;


            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            Add myform = new Add();
            DialogResult dr = myform.ShowDialog();
            if (dr == DialogResult.OK)
            {
                Book cbook = new Book(myform.Title, myform.Author, myform.Cost, myform.Photo);
                myform.fillBook(cbook); //заполняем книгу главами
                arTheBook.Add(cbook);
                UpdateGrid();

            }

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            String sfind;
            
            int ccoll = dataGridView1.SelectedCells[0].ColumnIndex;
            int crow = dataGridView1.SelectedCells[0].RowIndex;

            if (ccoll == 1)//название
            {
                int ind=-1;
                foreach(Book cbook in arTheBook)
                {
                    ind++;
                    sfind = cbook.author.ToLower();
                    if (sfind.StartsWith(textBox1.Text.ToLower()))
                    {
                        //dataGridView1.Focus(dataGridView1.Rows[ind].Cells[ccoll]);
                        //dataGridView1.Rows[ind].Cells[ccoll].Style.BackColor = Color.Red;
                        dataGridView1.Rows[crow].Cells[ccoll].Selected = false;
                        dataGridView1.Rows[ind].Cells[ccoll].Selected = true;
                        
                        return;

                    }
       
                    
                }


            }
            else if (ccoll == 2)//автор
            {
                int ind=-1;
                foreach (Book cbook in arTheBook)
                {
                    ind++;
                    sfind = cbook.title;
                    if (sfind.StartsWith(textBox1.Text))
                    {

                       //dataGridView1.Rows[ind].Cells[ccoll].Style.BackColor = Color.Red;
                        //dataGridView1.Rows[ind].Cells[ccoll].Selected = true;
                        dataGridView1.Rows[crow].Cells[ccoll].Selected = false;
                        dataGridView1.Rows[ind].Cells[ccoll].Selected = true;
                        return;

                    }
                }


            }
            //foreach (DataGridViewColumn column in dataGridView1.Columns)
            //{
            //    string value = dataGridView1.SelectedCells[0].Value.ToString();
                
            //    if (textBox1.Text == value)
            //    {
            //        textBox2.Text = value;
            //    }


            //string selectString = textBox1.Text;

            //DataRowCollection allRows = ((DataTable)dataGridView1.DataSource).Rows;

            //DataRow[] searchedRows =((DataTable)dataGridView1.DataSource).Select(selectString);

            //int rowIndex = allRows.IndexOf(searchedRows[0]);

            //dataGridView1.CurrentCell = dataGridView1[0, rowIndex];

      
}  

     



        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int cind = e.RowIndex;
            if (cind >= 0 && cind < arTheBook.Count)
            {
                main.cbook = (Book)arTheBook[cind];
            }

        }

        public void button3_Click(object sender, EventArgs e)
        {
            int ind = dataGridView1.SelectedCells[0].RowIndex;
            if (ind >= 0 && ind < arTheBook.Count)
            {

                arTheBook.RemoveAt(ind);
                UpdateGrid();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            int ind = dataGridView1.SelectedCells[0].RowIndex;
            if (ind >= 0 && ind < arTheBook.Count)
            {
                Add myform = new Add();
                Book cbook = (Book)arTheBook[ind];

                myform.Title = cbook.title;
                myform.Author = cbook.author;
                myform.Cost = cbook.cost;
                myform.Photo = cbook.photo;

                myform.GetChapterFromBook(cbook);
                DialogResult dr = myform.ShowDialog();


                if (dr == DialogResult.OK)
                {



                    cbook = new Book(myform.Title, myform.Author, myform.Cost, myform.Photo);
                    myform.fillBook(cbook);

                    arTheBook[ind] = cbook;

                    UpdateGrid();
                }

            }



        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
      
    

